package com.changba.songstudio.util;

public class FFmpegUtils {

    public static String VERSION =" 123";
    public static boolean loaded;
    private static String basePath = "";
    public static boolean isDebug;
    private static boolean isRuning;

    static {
        try{
            System.loadLibrary("cowpea-lib");
            System.loadLibrary("ijkffmpeg");
            loaded = true;
        }catch (Throwable e){
            //加载失败
            loaded = false;
        }
    }

    public static String getVideoRecordingFilePath() {
        return new StringBuilder(basePath).append("/").append(System.currentTimeMillis()).append(".mp4").toString();
    }

    public static boolean init(String path,boolean debug){
        basePath = path;
        isDebug = debug;
        setDebug(debug?1:0);
        //初始化基础路径
        return loaded;
    }

    public native static int runCmd(String[] commands);

    private native static int setDebug(int debug);

    public native static String stringFromJNI();

    public static int run(String[] cmd){
        if(!loaded || cmd==null || isRuning){
            return -1;
        }
        isRuning = true;
        int code = runCmd(cmd);
        isRuning = false;
        return code;
    }

    public static int run(String commands){
        if (commands != null) {
            String[] cmd = commands.split("[ \\t]+");
            return run(cmd);
        }
        return -1;
    }

}
